package com.toocms.jinjuhang.ui.mine.succour

import androidx.databinding.ObservableField
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.data.system.UploadData
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.MultiItemViewModel
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand

class ApplySuccourParamImageItemModel(
    viewModel: ApplySuccourModel,
    val type: String,
    val titleObservableField: ObservableField<String>
) : MultiItemViewModel<ApplySuccourModel>(viewModel) {
    val urlObservableField = ObservableField<String>()
    var id: String? = null

    val imageClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        viewModel.startSelectMultipleImageAty(null,
            1,
            object : OnResultCallbackListener<LocalMedia> {
                override fun onResult(result: ArrayList<LocalMedia>?) {
                    if (result.isNullOrEmpty()) return
                    val localMedia = result[0]
                    val path = localMedia.realPath
                    //上传图片
                    viewModel.requestData(
                        Urls.System.UPLOAD,
                        filePaths = mapOf(Pair("image", path)),
                        resultClass = UploadData::class.java,
                        isShowLoading = true,
                        result = {
                            it.list?.apply {
                                this@ApplySuccourParamImageItemModel.id = this[0].id
                                this@ApplySuccourParamImageItemModel.urlObservableField.set(this[0].abs_url)
                            }

                        })
                }

                override fun onCancel() {

                }

            })
    })


    init {
        itemType = Constants.RecyclerViewItemType.ITEM_TYPE_TWO
    }


}